package com.fBlog.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fBlog.pojo.*;
import com.fBlog.service.impl.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fBlog.service.impl.PhotoServiceI;
import com.fBlog.service.impl.SpeakServiceI;

@Controller
@RequestMapping("/pageController")
public class PageController {
	
	@Autowired
	private PhotoServiceI photoService;
	@Autowired
	private SpeakServiceI speakService;
	@Autowired
	private UserServiceI userService;

	@RequestMapping("/index")
	public String index(HttpServletRequest request, HttpServletResponse response,Model m){
		HttpSession session = request.getSession();
		User user =(User) session.getAttribute("user");
		//如果用户信息为空，不处理，直接交给拦截器拦截。
		if(user!=null){
			String familyId = user.getFamilyId();
			String userId = user.getId();

			//通过家庭id和用户id获取该用户所有的动态
			List<Speak> l = speakService.getThisUserSpeak(familyId,userId);
			m.addAttribute("thisUserSpeaks",l);
		}
		return "index";
	}
	
	
	@RequestMapping("/photo")
	public String photo(HttpServletRequest request, HttpServletResponse response,Model m){
		//获取用户id
		String id = request.getParameter("userId");
		//通过id获取用户相册。
		List<Alphoto> l = photoService.get(id);
		
		m.addAttribute("alphotos",l);
		
		return "photo";
	}
	
	@RequestMapping("/myphoto")
	public String myphoto(HttpServletRequest request, HttpServletResponse response,Model m){
		//获取相册id,name
		String id = request.getParameter("alphotoId");
		String name = request.getParameter("alphotoName");
		//通过相册id获取相册中所有照片。
		List<Photo> l = photoService.getPhoto(id);
		
		m.addAttribute("photos",l);
		
		m.addAttribute("alphotoId", id);

		m.addAttribute("alphotoName",name);
		
		return "Myphoto";
	}
	
	@RequestMapping("/dynamics")
	public String dynamics(HttpServletRequest request, HttpServletResponse response,Model m){
		//获取家庭id
		String familyId = request.getParameter("familyId");
		
		//通过家庭id获取家庭中所有成员发表的动态。
		List<Speak> l = speakService.getSpeaks(familyId);
		m.addAttribute("speaks",l);
		
		//获取每条动态下的所有评论，用map存放，key:动态id，value:评论的list集合
		Map<String,List<Comment>> map = speakService.getComments(l);
		m.addAttribute("comments", map);
		
		//获取每条评论下的所有回复，用map存放，key:评论id，value:回复的list集合
		Map<String,List<Comment>> map1 = speakService.getSelfComments(familyId);
		m.addAttribute("selfComments", map1);
		
		//将家庭id放进页面。
		m.addAttribute("familyId", familyId);
		
		return "Dynamics";
	}

	@RequestMapping("/myFamily")
	public String myFamily(HttpServletRequest request, HttpServletResponse response,Model m){
		HttpSession session = request.getSession();
		User user =(User) session.getAttribute("user");
		String familyId = user.getFamilyId();

		//通过家庭id获取该家庭所有用户
		List<User> l = userService.getUsers(familyId);
		m.addAttribute("users1",l);
		return "Myfamily";
	}
}
